

var utils = {
    getParams(name) { // 不传name返回所有值，否则返回对应值
        var url = window.location.search;
        if (url.indexOf('?') == 1) {
            return false;
        }
        url = url.substr(1);
        url = url.split('&');
        var name = name || '';
        var nameres;
        // 获取全部参数及其值
        for (var i = 0; i < url.length; i++) {
            var info = url[i].split('=');
            var obj = {};
            obj[info[0]] = decodeURI(info[1]);
            url[i] = obj;
        }
        // 如果传入一个参数名称，就匹配其值
        if (name) {
            for (var i = 0; i < url.length; i++) {
                for (const key in url[i]) {
                    if (key == name) {
                        nameres = url[i][key];
                    }
                }
            }
        } else {
            nameres = url;
        }
        // 返回结果
        return nameres;
    },

    setData(key, value) {
        if (typeof value == 'object') {
            let jsonStr = JSON.stringify(value);
            localStorage.setItem(key, jsonStr);
        } else {
            localStorage.setItem(key, value);
        }
    },

    getData(key) {
        let data = localStorage.getItem(key);
        try {
            return JSON.parse(data);
        } catch (error) {
            return data;
        }

    }
}



var service = axios.create({
    // baseURL: "http://huruqing.cn:3003",
    timeout: 50000, // 请求超时时间(因为需要调试后台,所以设置得比较大)
});

// request 对请求进行拦截
service.interceptors.request.use(
    (config) => {
        if (config.method === "post") {
            config.headers["content-Type"] = "multipart/form-data";
        }
        return config;
    },
    (error) => {
        Promise.reject(error);
    }
);

// response 响应拦截器
service.interceptors.response.use(
    (response) => {
        const res = response.data; 
        if (res.code == 666) {
            return res;
        } else {
            // 成功连接到后台, 但是没有返回正确的数据
            alert(res.msg);
        }
    },
    (error) => {
        alert('网络异常')
    }
);

var $ = {}
$.post = service.post;
$.get = (url, data = {}) => {
    return service.get(url, {
        params: data
    })
} 

